<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Setting extends Model
{
    use HasFactory;
    // 引入软删除
    use SoftDeletes;
    // 定义白名单
    protected $fillable = ['key','value','name','comment','sort'];

    // 定义类属性$kvs
    protected $kvs = null;
    // 
    public function kv(){
        if($this->kvs===null){
            // 借助mapWithKeys生成新的集合
            $this->kvs = $this->orderBy('sort','asc')->get()->mapWithKeys(function ($item){
                return[
                    $item['key'] => $item['value']
                ];
            });
        }
        return $this->kvs;
    }
}
